print("一个简单的字典alien_0\n")
alien_0={'color':'green','point':5}
print(alien_0,"\n\n")


print("通过键访问字典alien_0中的值\n")
print(alien_0['color'])
print(alien_0['point'],"\n\n")

print(f"you just earned {alien_0['point']} points! \n\n")

print("添加键值对")
print("给外星人alien_0添加x坐标和y坐标和speed \n")
alien_0['x_position']=0
alien_0['y-position']=25
alien_0['speed']='medium'
print(alien_0)

print('\n\n创建一个空字典alien_1，再在其中添加键值对\n')
alien_1={}
alien_1['color']='blue'
alien_1['point']=10
print(alien_1,'\n')

print('修改字典alien_0中的值\n')
print(f"the alien is {alien_0['color']}")
alien_0['color']='red'
print(f"the alien is now {alien_0['color']}\n")

print('向右移动的外星人，根据当前速度确定外星人移动多远')
if alien_0['speed']=="slow":
	x_new=1
elif alien_0['speed']=="medium":
	x_new=2
else:
	x_new=3
alien_0['x_position']=alien_0['x_position']+x_new
print(f"the alien's new position:{alien_0['x_position']}\n")


alien_0['speed']='fast'
if alien_0['speed']=="slow":
	x_new=1
elif alien_0['speed']=="medium":
	x_new=2
else:
	x_new=3
alien_0['x_position']=alien_0['x_position']+x_new
print(f"the alien's new position:{alien_0['x_position']}\n")


print('使用del永久删除不用的键值对\n')
print(alien_0)
del alien_0["speed"]
print(alien_0,"\n")

print('使用get()来访问值\n,如果要访问的键可能不存在，考虑使用get()方法，避免使用[]后报错')
print(alien_0.get("speed","no speed!"),"\n")
print(alien_0.get("speed"),"\n")     #get没有指定第二参数，且指定键不存在，则返回none

print('动手试一试，练习6.1-6.3\n')
print('练习6.1')
person={'first_name':'zhang','last_name':'san','age':20,'city':'sichuan'}
print(person)

print('练习6.2')
favorite_number={
					'zhangsan':6,
					'lisi':8,
					'wangwu':9,
					'wangmazi':5,
					'wenwen':1,
				}
print(favorite_number)
for k,v in favorite_number.items():
	print(f"{k}:{v}",end=" ")




print('\n练习6.3')
glossary={
			'tuple':'不可变的列表称为元组,使用圆括号来标识',
			'string':'就是一系列字符',
			'dic':'是一系列键值对，每个键都与一个值关联，可以使用键来访问与之关联的值',
			'variable':'变量常被描述为可用于存储值的盒子，是可以被赋值的标签，也可以说变量指向特定的值',
			'list':'由一系列按特定顺序排列的元素组成，用方括号表示列表，用逗号分隔其中元素',
		}
glossary['class']='定义了一组属性和方法，用于创建具有相同特性和行为的对象'
print(glossary)

for key,value in glossary.items():
	print(f"{key}:\n\t{value}")
print("\n")

for key,value in glossary.items():
	print(f"{key}:")
	print(f"{value}")


print('\n遍历所有的键值对\n')
user_0={
	'username':'efermi',
	'first':'enrico',
	'last':'fermi',
		}
for h,j in user_0.items():
	print(f'\nkey:{h}')
	print(f'value:{j}')
print("\n")

favorite_languages={
	'jen':'python',
	'sarah':'c',
	'edward':'rust',
	'phil':'python',
					}
for name,language in favorite_languages.items():
	print(f"{name.title()}'s favorite language'is {language.title()}")
print("\n")


print("遍历字典中的所有键,可使用keys()方法")
'''
for name_0 in favorite_languages.keys():
	print(name_0.title())
print("\n")
'''
for name_1 in favorite_languages:     #在遍历字典时，会默认遍历所有的键
	print(name_1.title())
print("\n")

friends=['phil','sarah']
for name_1 in favorite_languages:
	if name_1 in friends:
		language=favorite_languages[name_1].title()
		print(f"\t{name_1.title()},i see you love {language}")

if 'erin' not in favorite_languages.keys():
	print("erin,please take our poll!\n")

print("按特定顺序遍历字典中的所有键：使用sorted()方法")
for name_2 in sorted(favorite_languages.keys()):
	print(f"{name_2.title()},thank you for taking the poll.")

print("遍历字典中的所有值，可使用values()")
for language_1 in favorite_languages.values():
	print(language_1.title())


print("剔除字典中值的重复值，可使用set()方法")
for language_2 in set(favorite_languages.values()):
	print(language_2.title())


print('练习6.5')
rivers={'nile':'egypt','danube':'austria','the Yangtze River':'china'}
for river,country in rivers.items():
	print(f"the {river.title()} runs through {country.title()}")
print("\n")

for river_1 in rivers.keys():
	print(river_1)
print("\n")

for country_1 in rivers.values():
	print(country_1)
print("\n")

print("练习6.6")
'''
favorite_languages={
	'jen':'python',
	'sarah':'c',
	'edward':'rust',
	'phil':'python',
					}
'''
People_under_investigation=['jen','edward','amy']
for name_2 in favorite_languages.keys():
	if name_2 in People_under_investigation:
		print(f"{name_2.title()},Thank you for your participation in the survey.")
	elif name_2 not in People_under_investigation:
		print(f"{name_2.title()},Would you mind participating in this survey?")

print("\n嵌套：列表中嵌套字典，字典中嵌套列表，字典中嵌套字典")
print("一、列表中嵌套字典")
alien_0={'color':'green','point':5}
alien_1={'color':'yellow','point':10}
alien_2={'color':'red','point':15}
aliens=[alien_0,alien_1,alien_2]
print(aliens)
for alien in aliens:
	print(alien)


print("\n外星人不止三个，使用range()来生成众多的外星人")
aliens=[]
for alien_number in range(30):
	new_alien={'color':'green','point':5,'speed':'slow'}
	aliens.append(new_alien)
for alien in aliens:
	print(alien)
print(f"total number of aliens:{len(aliens)}")

print("\n修改前5个外星人的颜色，分数，速度")
for alien in aliens[:5]:
	if alien['color']=='green':
		alien['color']='yellow'
		alien['point']=10
		alien['speed']='meduim'
	elif alien['color']=='yellow':
		alien['color']='red'
		alien['point']=15
		alien['speed']='fast'
for alien_3 in aliens:
	print(alien_3)

print("\n二、在字典中存储列表")
pizza={
	'crust':'thick',
	'toppings':['mushrooms','extra cheese'],
	}
print(f"you ordered a{pizza['crust']}-crust pizza with the following toppings:")
for topping in pizza['toppings']:
	print(f"\t{topping}")

favorite_languages={
	'jen':['python','rust'],
	'sarah':['c'],
	'edward':['rust','go'],
	'phil':['python','haskell'],
					}
for name,language in favorite_languages.items():
	if len(language) >1:
		print(f"{name.title()}'s favorite languages are:")
		for language_4 in language:
			print(f"\t{language_4.title()}")
	else:
		for language_4 in language:
			print(f"{name.title()}'s favorite language is {language_4.title()}")
	
print("\n三、在字典中存储字典")
users={
		'aeinstein':{
				'first':'albert',
				'last':'einstein',
				'location':'princeton',
					},
		'mcurie':{
				'first':'marie',
				'last':'curie',
				'location':'paris',
				}
	}

for username,user_info in users.items():
	print(f"username:{username.title()}")
	print(f"\tfull name:{user_info['first'].title()} {user_info['last'].title()}")
	print(f"\tlocation:{user_info['location'].title()}")

print("\n练习6.7")
persons=[
			{'first':'zhang','last':'san','age':20,'city':'sichuan'},
			{'first':'li','last':'si','age':26,'city':'shanghai'},
			{'first':'wang','last':'lin','age':22,'city':'guangdong'},
		]

for person in persons:
	for person_info,person_info_1 in person.items():
		print(f"{person_info}:\t{person_info_1}")
print('\n')


for person in persons:
	print(f"name:\t{person['first'].title()} {person['last'].title()}\nage:\t{person['age']}\ncity:\t{person['city'].title()}")


print('\n练习6.8')
pets=[]


print('\n练习6.9')
favorite_places={
					'zahngsan':['chuanxi','xian'],
					'lisi':['shanghai','qinghai'],
					'wangwu':['guangdong','paris','hainan']
				}
for name,places in favorite_places.items():
	print(f"{name.title()} favorite places are:")
	for place in places:
		print(f"\t{place}")



